#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}.basic.security.oauth2.handler;

import ${package}.common.bean.vo.JsonResult;
import ${package}.common.emum.ReturnCodeEnum;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;
import org.springframework.stereotype.Component;

/**
 * Description:
 *   oAuth2 Exception 自定义处理类
 *
 * @author wupanhua
 * @date 2020-05-11 14:42
 *
 * <pre>
 *              ${copyright}
 *      Copyright (c) 2019. All Rights Reserved.
 * </pre>
 */
@Component
public class YkoAuthExceptionTranslator implements WebResponseExceptionTranslator {


    @Override
    public ResponseEntity translate(Exception e) {

        // 验证失败
        if (e instanceof OAuth2Exception || e instanceof  AuthenticationException) {
            return ResponseEntity.status(HttpStatus.NON_AUTHORITATIVE_INFORMATION).body(JsonResult.getInstant(ReturnCodeEnum.LOGIN_FAIL, e.getMessage()));
        }

        // 返回成功信息
        return ResponseEntity.status(HttpStatus.OK).body(JsonResult.getInstant(ReturnCodeEnum.SYSTEM_ERROR, e.getMessage()));
    }

}
